package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.SaleTimesListResponse
import com.gexiaobao.pigeon.app.util.DatetimeUtil
import com.gexiaobao.pigeon.databinding.ItemFlashSaleTimes2Binding
import com.gexiaobao.pigeon.databinding.ItemFlashSaleTimesBinding

/**
 * @Author      : hxw
 * @Date        : 2024/1/27 14:59
 * @Describe    :
 */
class FlashSaleTimesAdapter2(data: ArrayList<SaleTimesListResponse.ListBean>, timesId: Int) :
    BaseQuickAdapter<SaleTimesListResponse.ListBean, DataBindBaseViewHolder>(R.layout.item_flash_sale_times2, data) {

    private var mId = 0

    init {
        mId = timesId
    }

    /** 首先在RecyclerView的Adapter中定义一个变量来保存选中的位置 */
    private var selectedPosition = mId
    private var mOnItemClickListener: OnTimesItemClickListener? = null

    @SuppressLint("NotifyDataSetChanged")
    override fun convert(holder: DataBindBaseViewHolder, item: SaleTimesListResponse.ListBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemFlashSaleTimes2Binding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        val type = when (item.state) {//1-未开始2-进行中3-已结束
            1 -> context.getString(R.string.About_to_take_the_gun)//"即将开抢"
            2 -> context.getString(R.string.Promote_events)//"活动进行"
            3 -> context.getString(R.string.activity_end)//"活动结束"
            else -> context.getString(R.string.unkown)//"暂无"
        }

        val llItemView = holder.getView<LinearLayout>(R.id.llItemView)

        holder.setText(R.id.tvItemActivityType, type)

        holder.itemView.setOnClickListener {
            /** 获取当前点击的位置 */
            val position = holder.adapterPosition
            /** 如果当前位置和之前选中的位置不同，则更新选中位置，并刷新RecyclerView */
            if (position != selectedPosition) {
                selectedPosition = position
                notifyDataSetChanged()
            }
            mOnItemClickListener?.onItemClick(item.id)
        }
        if (selectedPosition == holder.adapterPosition) {
            llItemView.setBackgroundColor(ContextCompat.getColor(context, R.color.color_fe4542))
            llItemView.scaleX = 1.1f
            llItemView.scaleY = 1.1f
            holder.setGone(R.id.tvSeatWhite, true)
        } else {
            llItemView.setBackgroundColor(ContextCompat.getColor(context, R.color.comm_text_gray_dark))
            llItemView.scaleX = 1.0f
            llItemView.scaleY = 1.0f
            holder.setGone(R.id.tvSeatWhite, false)
        }
    }

    fun setOnTimesClickListener(listener: OnTimesItemClickListener?) {
        mOnItemClickListener = listener
    }

    interface OnTimesItemClickListener {
        fun onItemClick(id: Int)
    }
}